//
//  CHSOffScreenDrawController.m
//  ZXAnimationUtil_Example
//
//  Created by 朱新明 on 2023/9/11.
//  Copyright © 2023 simon. All rights reserved.
//

#import "CHSOffScreenDrawController.h"
#import <ZXCategoriesUtil/CAICategories.h>
#import <Masonry/Masonry.h>

@interface CHSOffScreenDrawController ()

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UILabel *titleLab;

@property (nonatomic, strong) UIButton *offscreenBtn;

@property (nonatomic, strong) UIImageView *onscreenView;


@end

@implementation CHSOffScreenDrawController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"离屏渲染";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUI];
    [self addMas];
    [self setData];
}


- (void)setUI{
    
    
    [self.view addSubview:self.scrollView];
    
    [self.scrollView addSubview:self.titleLab];


    [self.scrollView addSubview:self.offscreenBtn];
    [self.scrollView addSubview:self.onscreenView];
}


- (void)addMas{
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset([UIApplication sharedApplication].zx_safeAreaStatusBarHeight+44);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view.mas_left);
    }];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.scrollView.mas_top);
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.scrollView.mas_left).offset(15);
    }];

    [self.offscreenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.scrollView.mas_left).offset(50);
        make.centerX.mas_equalTo(self.scrollView);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(20);
        make.height.mas_equalTo(60);
    }];
    
    [self.onscreenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.scrollView.mas_left).offset(50);
        make.centerX.mas_equalTo(self.scrollView);
        make.top.mas_equalTo(self.offscreenBtn.mas_bottom).offset(20);
        make.height.mas_equalTo(60);
        make.bottom.mas_equalTo(self.scrollView.mas_bottom).offset(-10);
    }];
}

- (void)setData{
    self.titleLab.text = @"视图的渲染分为两种方式：当屏渲染和离屏渲染。\n On-Screen Rendering：当屏渲染是指GPU的渲染操作在当前显示的 屏幕缓存区 中进行渲染。\nOff-Screen Rendering：离屏渲染分CPU离屏渲染和GPU离屏渲染。\nGPU离屏渲染：是指GPU在当前显示的 屏幕缓存区 外开辟一个新的缓存区进行渲染。需要我们预防的。\nCPU离屏渲染：离屏渲染本身是图形渲染技术中的一种优化。在CUP的离屏渲染中，CoreG的位图图形上下文、PDF图形上下文、CGLayer图层图形上下文的绘制都是离屏绘制。例如如果您希望离屏渲染图像(稍后绘制，或保存到文件中)，则必须创建位图图像上下文。创建一个位图图像上下文，使用UIKit框架或Core Graphics函数来绘制它，然后从上下文获得一个图像对象。UIGraphicsBeginImageContextWithOptions。\n（1）GPU的离屏渲染如何触发的，何时会触发？在遇到渲染的图像需要做许多预处理才能渲染，就会触发GPU的离屏渲染。如设置使用了图片的UIButton等组件的圆角，对图层进行裁剪，mask蒙板、阴影等操作时都会触发离屏渲染。（2）它对性能有怎样的影响？为什么要避免GPU离屏渲染？\n遇到GPU离屏渲染的时候，首先需要在当前显示的屏幕缓冲区外创建一个新的缓存区进行渲染操作；其次，当离屏渲染结束后，要将离屏渲染的内容在屏幕上显示，需要进行图形上下文的切换，这个过程非常消耗性能。尤其是在当前屏幕使用大量圆角组件时。\n所以要合理的使用离屏渲染，最简单的方式是直接使用圆角图片，不在代码层面做裁剪操作。";
    
}


- (UIScrollView *)scrollView{
    if (!_scrollView) {
        UIScrollView *view = [[UIScrollView alloc] init];
        view.backgroundColor = [UIColor redColor];
        _scrollView = view;
    }
    return _scrollView;
}

- (UILabel *)titleLab
{
    if (!_titleLab) {
        UILabel *lab = [[UILabel alloc] init];
        lab.font = [UIFont zx_systemFontOfSize:16];
        lab.text = NSLocalizedString(@"", nil);
        lab.textColor = [UIColor blackColor];
        lab.numberOfLines = 0;
        _titleLab = lab;
    }
    return _titleLab;
}

- (UIButton *)offscreenBtn
{
    if (!_offscreenBtn) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"keyframing_2"] forState:UIControlStateNormal];
        NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"微信登录" attributes:@{NSForegroundColorAttributeName:[UIColor zx_colorWithHexString:@"#666666"],NSFontAttributeName: [UIFont zx_systemFontOfSize:12]}];
        [btn setAttributedTitle:str forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor zx_colorWithHexString:@"#E83E00"];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 10;
        _offscreenBtn = btn;
    }
    return _offscreenBtn;
}

- (UIImageView *)onscreenView
{
    if (!_onscreenView) {
        UIImageView *btn = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"keyframing_2"]];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 10;
        _onscreenView = btn;
    }
    return _onscreenView;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
